<template>
  <div>
    <button @click="requestObjectMap">发送请求1</button>
    <button @click="requestObjectMap2">发送请求2</button>
    <button @click="requestInsertBack">发送请求3</button>
  </div>
</template>


<script>

export default {
  data() {
    return {
      data: null, // 属性data用于存储请求返回来的数据，默认值null
    };
  },
  methods: {
    requestObjectMap() {
      let obj = this.createObject()
      let json = JSON.stringify(obj)
      console.log(obj);
    },

    requestObjectMap2() {
      let objs = this.createIdObject();
      let json_lis = this.createMemoryMap(objs);
      let json = JSON.stringify(json_lis);
      console.log(json_lis)
    },

    requestInsertBack() {
      let res = this.createExternal();
      let y = res.y;
      let objs = res.objs;
      let json_lis = this.createMemoryMap(objs);
      let json = JSON.stringify(json_lis);
      console.log(json_lis);
      // 发送
    },

    createMemoryMap(objs) {
      // 遍历每一个对象，加上id
      let json_lis = [];
      objs.forEach(obj => {
        let temp = {
          id: obj.id,
          name: obj.name,
          fID: (obj.f) ? obj.f.id : 0,
          gID: (obj.g) ? obj.g.id : 0
        };
        json_lis.push(temp)
      });
      return json_lis;
    },

    createIdObject() {
      // 首先要给每一个对象唯一个编号，加一个字段id
      let d = {id: 4, name: 'd'};
      let b = {id: 2, name: 'b', f: d};
      let c = {id: 3, name: 'c', f: d};
      let a = {id: 1, name: 'a', f: b, g: c};
      return [a, b, c, d];
    },

    createObject() {
      let d = {name: 'd'};
      let b = {name: 'b', f: d};
      let c = {name: 'c', f: d};
      return {name: 'a', f: b, g: c};
    },

    createExternal() {
      let objs = this.createIdObject();
      // 创建一个外部对象y，指向b对象
      return {
        y: {f: objs[1], fID: objs[1].id},
        objs: objs
      }
    }

  },

};
</script>

<style>
</style>
